home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1256 / c_filt.c_ / c_filt.c
C/C++ Source or Header  |  1997-04-18  |  16KB  |  376 lines

  1. /* EasyCODE(C++) V5.1 01.03.1995 16:22:17 */
  2. /* EasyCODE O
  3. If=vertical
  4. LevelNumbers=no
  5. LineNumbers=no
  6. ScreenFont=Courier New,,80,9220,-11,0,400,0,0,0,0,0,0,3,2,1,49
  7. PrinterFont=Courier New,,80,17414,-34,0,400,0,0,0,0,0,0,3,2,1,49
  8. LastLevelId=22 */
  9.  
  10. /* EasyCODE ( 1
  11.    c_filt.c */
  12. #include "c_filt.h"     // include header files
  13.  
  14. /* EasyCODE ( 15
  15.    CommentGetChar */
  16.  
  17. /* EasyCODE F */
  18. char CommentGetChar()
  19. /********************************************************************************************************************
  20. **              Function:   CommentGetChar
  21. **              Parameters: none
  22. **              Return:     1 character
  23. **    Purpose:  Function delivers charecters of a string. This string contains
  24. **              the latest line read, but without keyword. 
  25. ********************************************************************************************************************/
  26.    {
  27.    return i_buf[ bufPos++];
  28.    }
  29. /* EasyCODE ) */
  30.  
  31. /* EasyCODE ( 16
  32.    CommentFilter */
  33.  
  34. /* EasyCODE F */
  35. int CommentFilter( int *convertReturn, FILE *stream)
  36. /****************************************************************************************************************
  37. **            Function:   CommentFilter
  38. **            Parameters: convertReturn defines the "Return" processing
  39. **                        output stream
  40. **            Return:     Integer error code
  41. **  Purpose:  The function checks whether the line contains (C/C++)-Kommentare enthalten and extracts
  42. **            them. Each comment is written to a separate line. If a comment is not closed within the
  43. **            the same text (i.e. between the corresponding Text-EndText keys), an error message is
  44. **            created. Also if a comment is closed that has no begin, an error message is created.
  45. **            Comments are written to the output stream contained in the parameter stream.
  46. **            The value of *convertReturn defines the "Return" processing:
  47. **            0: Comment is written "normally" in "Line=..".
  48. **            1: Blanks are inserted between Line= and comment.
  49. **            2: "Return" is inserted between Line= and comment.
  50. ****************************************************************************************************************/
  51.    {
  52.    char ch, last_ch;                    
  53.    int  startpos=0;        // Variable for comment begin
  54.    BOOL readchar = FALSE;  // Flag, indicating read ahaed of character
  55.    bufPos = 0;             // Initialization of actual reading position
  56.    cInString = '\0';       // Indicator for position (string, comment, statement).
  57.    /* EasyCODE - */
  58.    ch = CommentGetChar();            // Read one character
  59.    while ((ch != '\0') && (ch != '\r') && (ch != '\n')
  60.           /* while not EOL */)
  61.       {
  62.       if (ch == EOF
  63.           /* EOF reached */)
  64.          {
  65.          return ERROR_EOF /* Return error */;
  66.          }
  67.       switch (ch
  68.               /* action dependent on character read */)
  69.          {
  70.          case '\"' /* Character read: " */:
  71.             if ((! bInBlockComment) && (! bInLineComment)
  72.                 /* If not within a C or C++ comment */)
  73.                {
  74.                if (cInString == '\"'
  75.                    /* Current position within string ".." */)
  76.                   {
  77.                   cInString = '\0';
  78.                   /* close string on second " - reset cInString to \0 */
  79.                   }
  80.                else
  81.                   {
  82.                   cInString = '\"';
  83.                   /* begin of a string */
  84.                   }
  85.                startpos++;
  86.                /* Within a string the start position is incremented 
  87.                   because this part is not taken */
  88.                }
  89.             break;
  90.          case '\'' /* Character read: ' */:
  91.             if ((! bInBlockComment) && (! bInLineComment)
  92.                 /* If not within a C or C++ comment */)
  93.                {
  94.                if (cInString == '\''
  95.                    /* Current position within string '..' */)
  96.                   {
  97.                   cInString = '\0';
  98.                   /* Bei zweitem auftretenden ' muß der String geschlossen und die 
  99.                      Hilfsvariable cInString auf \0 zurückgesetzt werden */
  100.                   }
  101.                else
  102.                   {
  103.                   cInString = '\'';
  104.                   /* begin of a string */
  105.                   }
  106.                startpos++;
  107.                /* Within a string the start position is incremented 
  108.                   because this part is not taken */
  109.                }
  110.             break;
  111.          case '/'   /* Character read: / */:
  112.             if (cInString == '\0'
  113.                 /* Current position outside a string ? */)
  114.                {
  115.                switch (ch = CommentGetChar()
  116.                        /* Read next character and do appropriate actions */)
  117.                   {
  118.                   case '*' /* Character read: * */:
  119.                      if ((!bInLineComment)
  120.                          /* Outside C++ comment */)
  121.                         {
  122.                         if (!bInBlockComment)
  123.                            {
  124.                            bInBlockComment = TRUE;
  125.                            /* Boolean variable for block comments is now TRUE until
  126.                               ending mark is reached */
  127.                            }
  128.                         else
  129.                            {
  130.                            readchar = TRUE;
  131.                            }
  132.                         }
  133.                      break;
  134.                   case '/'/* Character read: / */:
  135.                      if (!bInBlockComment
  136.                          /* Outside block comment */)
  137.                         {
  138.                         bInLineComment = TRUE;   // Set flag for C++ comments
  139.                         do
  140.                            {
  141.                            last_ch = ch;                // Save latest charcter read
  142.                            ch = CommentGetChar();       // Read next character
  143.                            }
  144.                         while (((ch != '\0') && (ch != '\r') && (ch != '\n'))
  145.                                /* While not EOL */);
  146.                         switch (*convertReturn
  147.                                 // "Return" processing necessary ?
  148.                                )
  149.                            {
  150.                            case 2  // First line of "Return" statement to be written
  151.                                :
  152.                               fputs("Line=Return ", stream);     
  153.                               fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  154.                               fputs("\n",stream);
  155.                               // Insert "Return" in first "Line=" line before content of line. 
  156.                               
  157.                               *convertReturn = 1;  
  158.                               // Continue with another return processing.
  159.                               break;
  160.                            case 1:
  161.                               fputs("Line=       ", stream);     
  162.                               fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  163.                               fputs("\n",stream);
  164.                               // Insert blanks after "Line=" to achieve proper alignment.
  165.                               break;
  166.                            case 0:
  167.                               fputs("Line=", stream);     
  168.                               fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  169.                               fputs("\n",stream);
  170.                               /* Write comment without any modifications. */
  171.                               break;
  172.                            }
  173.                         if (last_ch != '\\'
  174.                             /* Last character before EOL no \ ?*/)
  175.                            {
  176.                            bInLineComment = FALSE;  // Reset flag.
  177.                            }
  178.                         else
  179.                            {
  180.                            /* If last character "\", next line is a comment, too. */
  181.                            }
  182.                         return NO_ERROR
  183.                                /* Comment processed successfully */;
  184.                         }
  185.                      break;
  186.                   default:
  187.                      if ((! (bInBlockComment)) && (! (bInLineComment))
  188.                          /* If not within a comment */)
  189.                         {
  190.                         startpos += 1;    // Increment comment position
  191.                         }
  192.                      readchar = TRUE;    // Set flag for read ahead.
  193.                      break;
  194.                   }
  195.                }
  196.             else
  197.                {
  198.                startpos++;    // Increment comment position if within a string.
  199.                }
  200.             break;
  201.          case '\\' /* Character read: \ */:
  202.             if ((! bInBlockComment) && (! bInLineComment)
  203.                 /* Outside any comment */)
  204.                {
  205.                ch = CommentGetChar();       
  206.                /* Skip next character */
  207.                /* EasyCODE - */
  208.                startpos += 2;     // Increment comment position
  209.                }
  210.             break;
  211.          case '*' /* Character read: * */:
  212.             if (cInString == '\0')
  213.                {
  214.                if ((ch = CommentGetChar()) == '/'
  215.                    /* If next charcter is / */)
  216.                   {
  217.                   if (bInBlockComment
  218.                       /* If within C comment */)
  219.                      {
  220.                      switch (*convertReturn
  221.                              // If "Return" processing necessary
  222.                             )
  223.                         {
  224.                         case 2  // First "Line=" line of a  Return statement to be written
  225.                             :
  226.                            fputs("Line=Return ", stream);     
  227.                            fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos  - startpos), stream);
  228.                            fputs("\n",stream);
  229.                            // Insert "Return" in first "Line=" line before content of line. 
  230.                            
  231.                            *convertReturn = 1;  
  232.                            // Continue with another return processing.
  233.                            break;
  234.                         case 1:
  235.                            fputs("Line=       ", stream);     
  236.                            fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - startpos), stream);
  237.                            fputs("\n",stream);
  238.                            // Insert blanks after "Line=" to achieve proper alignment.
  239.                            break;
  240.                         case 0:
  241.                            fputs("Line=", stream);     
  242.                            fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - startpos), stream);
  243.                            fputs("\n",stream);
  244.                            /* Write comment without any modifications. */
  245.                            break;
  246.                         }
  247.                      /* Write comment */
  248.                      /* EasyCODE - */
  249.                      bInBlockComment = FALSE;       
  250.                      startpos = bufPos;           
  251.                        // Reset flag for C comments.
  252.                        // Set comment psoition to current position.
  253.                      }
  254.                   else
  255.                      {
  256.                      if (! bInLineComment
  257.                          /* If not within a C++ comment */)
  258.                         {
  259.                         return ERROR_COMMENT
  260.                                /* Error message if comment did not start properly. */;
  261.                         }
  262.                      else
  263.                         {
  264.                         readchar = TRUE;    // Unknown character read ahead.
  265.                         }
  266.                      }
  267.                   }
  268.                else
  269.                   {
  270.                   if (( ! bInBlockComment) && (! bInLineComment)
  271.                       /* Outside C or C++ comment. */)
  272.                      {
  273.                      startpos += 1;   // Increment comment psoition.
  274.                      }
  275.                   readchar = TRUE;    // Unknown character read ahead.
  276.                   }
  277.                }
  278.             else
  279.                {
  280.                startpos++;
  281.                }
  282.             break;
  283.          default:
  284.             if ((! bInBlockComment) && (! bInLineComment)
  285.                 /* Outside C or C++ comment */)
  286.                {
  287.                startpos++;     // Increment comment position.
  288.                }
  289.             break;
  290.          }
  291.       if (! readchar
  292.           /* No character read ahead */)
  293.          {
  294.          last_ch = ch;               // Save last character (needed for C++ comments).
  295.          ch = CommentGetChar();      // Read next character.
  296.          }
  297.       readchar = FALSE;   // Reset flag for read ahead.
  298.       }
  299.    if (bInBlockComment
  300.        /* Within C comment */)
  301.       {
  302.       /* You'll get here if a C comment is open. 
  303.          This comment is written to the output file. */
  304.       switch (*convertReturn
  305.               // "Return" processing necessary
  306.              )
  307.          {
  308.          case 2  // First "Line=" line of a  Return statement to be written
  309.              :
  310.             fputs("Line=Return ", stream);     
  311.             fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  312.             fputs("\n", stream);
  313.             // Insert "Return" in first "Line=" line before content of line. 
  314.             
  315.             *convertReturn = 1;  
  316.             // Continue with another return processing.
  317.             break;
  318.          case 1:
  319.             fputs("Line=       ", stream);     
  320.             fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  321.             fputs("\n", stream);
  322.             // Insert blanks after "Line=" to achieve proper alignment.
  323.             break;
  324.          case 0:
  325.             fputs("Line=", stream);     
  326.             fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  327.             fputs("\n", stream);
  328.             /* Write comment without any modifications. */
  329.             break;
  330.          }
  331.       }
  332.    if (bInLineComment
  333.        /* Within C++ comment */)
  334.       {
  335.       /* C++ comment has multiple lines;  
  336.          first line is written to Output file */
  337.       switch (*convertReturn
  338.               // "Return" processing necessary
  339.              )
  340.          {
  341.          case 2  // First "Line=" line of a  Return statement to be written
  342.              :
  343.             fputs("Line=Return ", stream);     
  344.             fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  345.             fputs("\n", stream);
  346.             // Insert "Return" in first "Line=" line before content of line. 
  347.             
  348.             *convertReturn = 1;  
  349.             // Continue with another return processing.
  350.             break;
  351.          case 1:
  352.             fputs("Line=       ", stream);     
  353.             fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  354.             fputs("\n", stream);
  355.             // Insert blanks after "Line=" to achieve proper alignment.
  356.             break;
  357.          case 0:
  358.             fputs("Line=", stream);     
  359.             fwrite( (void*) &i_buf[startpos], sizeof(char), (bufPos - 1 - startpos), stream);
  360.             fputs("\n", stream);
  361.             /* Write comment without any modifications. */
  362.             break;
  363.          }
  364.       if (last_ch != '\\'
  365.           /* Last character in C++ comment not \ */)
  366.          {
  367.          bInLineComment = FALSE;
  368.          /* If last character not \ C++ comment is finished. */
  369.          }
  370.       }
  371.    return NO_ERROR
  372.           /* Return value for correct comment processing */;
  373.    }
  374. /* EasyCODE ) */
  375. /* EasyCODE ) */
  376.